package com.hellpro.lolbox.splash;

import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

import com.hellpro.lolbox.MainActivity;
import com.hellpro.lolbox.R;
import com.hellpro.lolbox.utils.UIManger;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class SplashActivity extends AppCompatActivity {

    @BindView(R.id.iv_welcome)
    ImageView ivWelcome;
    @BindView(R.id.iv_splash)
    ImageView ivSplash;
    private SharedPreferences sp;
    private Unbinder mBind;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        mBind = ButterKnife.bind(this);//黄油刀的绑定
        initView();
        //隐藏顶部状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    public void initView() {
        sp = getSharedPreferences("isLogin", MODE_PRIVATE);
        final boolean isFirst = sp.getBoolean("isFirst", true);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isFirst) {
                    SharedPreferences.Editor edit = sp.edit();
                    edit.putBoolean("isFirst", false);
                    edit.apply();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            ivWelcome.setVisibility(View.GONE);
                            ivSplash.setVisibility(View.VISIBLE);
                            ivSplash.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    UIManger.startToActivity(SplashActivity.this, MainActivity.class);
                                    finish();
                                }
                            });
                        }
                    }, 2000);

                } else {
                    UIManger.startToActivity(SplashActivity.this, MainActivity.class);
                    finish();
                }
            }
        }, 2000);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mBind != null) {
            mBind.unbind();
        }
    }
}
